package com.tulun.service;

import com.tulun.job.CompressJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Date;

/**
 * desc: 任务调度服务
 * @user:gongdezhe
 * @date:2018/8/19
 */
public class QuartzService {
    private static String defaultGroup = "MyGroup";
    private static String cron ;
    private String defaultCron = "0/1 * * * * ?"; //默认每秒执行一次
    private static String path;
    private boolean isCompress;
    private boolean defaultCompress = true;//支持文件压缩或者解压，默认是进行压缩

    /**
     * 构造函数
     * @param path：
     * @throws Exception
     */
    public QuartzService(String path) throws Exception {
      new QuartzService(path, defaultCron, defaultCompress);
    }

    /**
     * 构造函数
     * @param path
     * @param cron
     * @throws Exception
     */
    public QuartzService(String path , String cron) throws Exception {
        new QuartzService(path, cron, defaultCompress);
    }

    /**
     * 构造函数
     * @param path
     * @param isCompress
     * @throws Exception
     */
    public QuartzService(String path , boolean isCompress) throws Exception {
        new QuartzService(path, defaultCron, isCompress);
    }

    /**
     *
     * @param path 路径
     *    压缩目录或者文件路径【该压缩后缀名为.tulun】
     *    若该路径是目录，则压缩该路径下所有待压缩文件（文件非.tulun结尾）
     *    若改路径是文件且未压缩，则直接压缩位.tulun结尾
     * @param cron cron表达式
     * @param isCompress 解压，压缩
     * @throws Exception
     */
    public QuartzService(String path , String cron, boolean isCompress) throws Exception {
        //参数校验
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("该目录或者文件路径不存在");
            new FileNotFoundException("该目录或者文件路径不存在");
            return;
        }

        //cron表达式效验
        new CronExpression(cron);

        this.path = path;
        this.cron = cron;
        this.isCompress = isCompress;

    }

    /**
     * 任务调度
     */
    public void schedule() {
        try{
            // 创建Scheduler
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();

            // 创建Job的实例
            JobDetail job = JobBuilder.newJob(CompressJob.class)
                    .withIdentity(CompressJob.class.getName(), defaultGroup)
                    .build();
            //向job里面传递参数
            job.getJobDataMap().put("path", this.path);
            job.getJobDataMap().put("isCompress", this.isCompress);

            System.out.println(cron);
            // 创建Trigger:基于cron表达式实现的触发器
            CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger()
                    .withIdentity("Trigger", defaultGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(this.cron))
                    .build();


            //将trigger和jobdetail加入这个调度
            scheduler.scheduleJob(job, trigger);
            // 调度启动
            scheduler.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
